﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StuRecruitSysBLL;
using System.Data.SqlClient;
using System.Data;
using System.Text;

namespace StuRecruitSys
{

    public partial class CashierHome: System.Web.UI.Page
    {

        private static string connString = "Data Source=.;Initial Catalog=StuRecruitDB;User ID=sa;Pwd=123";
        SqlConnection sqlCon = new SqlConnection();


        public void Page_Load(object sender, EventArgs e)
        {
            //学生基本信息
            using (SqlConnection sqlCon = new SqlConnection())
            {
                sqlCon.ConnectionString = connString;
                string strSql = "select * from student";
                SqlDataAdapter da = new SqlDataAdapter(strSql, sqlCon);
                DataSet ds = new DataSet();
                da.Fill(ds, "student");
                GridViewStu.DataSource = ds;
                GridViewStu.DataBind();
            }


            //账款
            using (SqlConnection sqlCon = new SqlConnection())
            {
                sqlCon.ConnectionString = connString;
                string strSql = "select * from v_findFinance";
                SqlDataAdapter da = new SqlDataAdapter(strSql, sqlCon);
                DataSet ds = new DataSet();
                da.Fill(ds, "v_findFinance");
                GridViewPay.DataSource = ds;
                GridViewPay.DataBind();
            }


            //账款
            using (SqlConnection sqlCon = new SqlConnection())
            {
                sqlCon.ConnectionString = connString;
                string strSql = "select * from v_print";
                SqlDataAdapter da = new SqlDataAdapter(strSql, sqlCon);
                DataSet ds = new DataSet();
                da.Fill(ds, "v_print");
                GridViewPrint.DataSource = ds;
                GridViewPrint.DataBind();
            }
        }
        //查询学生
        private string getSqlStringStu(string condition)
        {
            string strSql = "select * from student";
            if (condition == "---全部---")
            {
                strSql = "select * from student";
            }
            else if (condition == "学号")
            {
                strSql = "select * from student where sno='" + textSearchStu.Value + "'";
            }
            else if (condition == "姓名")
            {
                strSql = "select * from student where sname='" + textSearchStu.Value + "'";
            }
            else if (condition == "专业")
            {
                strSql = "select * from student where mname='" + textSearchStu.Value + "'";
            }
            else if (condition == "年级")
            {
                strSql = "select * from student where sgrade='" + textSearchStu.Value + "'";
            }
            else if (condition == "班级")
            {
                strSql = "select * from student where sclass='" + textSearchStu.Value + "'";
            }
            return strSql;

        }
        //查询学生信息
        protected void btnSearchStu_onclick(object sender, EventArgs e)
        {
            string condition = DropDownListStu.Text.Trim();
            string sqlStr = getSqlStringStu(condition);
            //查询应收账款
            using (SqlConnection sqlCon = new SqlConnection())
            {
                sqlCon.ConnectionString = connString;
                SqlDataAdapter da = new SqlDataAdapter(sqlStr, sqlCon);
                DataSet ds = new DataSet();
                da.Fill(ds, "student");
                GridViewStu.DataSource = ds;

                GridViewStu.DataBind();
            }
        }

        //查询账款
        private string getSqlStringPay(string condition)
        {
            string strSql = "select * from v_findFinance";
            if (condition == "---全部---")
            {
                strSql = "select * from v_findFinance";
            }
            else if (condition == "学号")
            {
                strSql = "select * from v_findFinance where sno='" + textSearchPay.Value + "'";
            }
            else if (condition == "姓名")
            {
                strSql = "select * from v_findFinance where sname='" + textSearchPay.Value + "'";
            }
            else if (condition == "专业")
            {
                strSql = "select * from v_findFinance where mname='" + textSearchPay.Value + "'";
            }
            else if (condition == "班级")
            {
                strSql = "select * from v_findFinance where sclass='" + textSearchPay.Value + "'";
            }
            return strSql;

        }
        //查询应收账款
        protected void btnSearchPay_onclick(object sender, EventArgs e)
        {
            string condition = DropDownListPay.Text.Trim();
            string sqlStr = getSqlStringPay(condition);
            //查询应收账款
            using (SqlConnection sqlCon = new SqlConnection())
            {
                sqlCon.ConnectionString = connString;
                SqlDataAdapter da = new SqlDataAdapter(sqlStr, sqlCon);
                DataSet ds = new DataSet();
                da.Fill(ds, "v_findFinance");
                GridViewPay.DataSource = ds;
                GridViewPay.DataBind();
            }
        }

        //出纳确认信息无误
        protected void btnSure_onclick(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(connString);
            string sql = string.Format("update financedept set cusername='{0}' ", StuRecruitSysDAL.cuser.uname);
            try
            {
                SqlCommand command1 = new SqlCommand(sql, conn);  // 创建command对象 
                conn.Open();  // 打开数据库连接
                int result = command1.ExecuteNonQuery();  // 执行命令
                if (result < 1)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('确认信息失败，请重试！');</script>");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('您已成功确认信息！');</script>");
                }
            }
            finally
            {
                conn.Close();  // 关闭数据库连接
            }
        }

        //打印
        protected void btnPrint_onclick(object sender, EventArgs e)
        {
             Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('请接入打印机接口');</script>");   
        }
    }
}